package com.nature.ffmpegforandroid

import android.view.Surface

/**
 *User: RandBII
 *Date: 2020/12/29
 *Description: 视频播放器
 */
class NatureMediaPlayer {

    private var mNativePlayHandle: Long = 0
    var mEventCallback: EventCallback? = null

    companion object {
        init {
            System.loadLibrary("ffmpeg-android")
        }

        //gl 渲染器
        const val VIDEO_GL_RENDER = 0
        const val AUDIO_GL_RENDER = 1
        const val VR_3D_GL_RENDER = 2

        //消息类型
        const val MSG_DECODER_INIT_ERROR = 0
        const val MSG_DECODER_READY = 1
        const val MSG_DECODER_DONE = 2
        const val MSG_REQUEST_RENDER = 3
        const val MSG_DECODING_TIME = 4

        //视频参数
        const val MEDIA_PARAM_VIDEO_WIDTH = 0X0001
        const val MEDIA_PARAM_VIDEO_HEIGHT = 0X0002
        const val MEDIA_PARAM_VIDEO_DURATION = 0X0003

        //界面渲染类型
        const val VIDEO_RENDER_OPENGL = 0
        const val VIDEO_RENDER_ANWINDOW = 1
        const val VIDEO_RENDER_3D_VR = 2

        external fun native_set_gesture(renderType: Int, x: Float, y: Float, scale: Int)

        external fun native_set_touchloc(renderType: Int, touchx: Float, touchY: Float)


    }


    interface EventCallback {
        fun onPlayerEvent(msgType: Int, msgValue: Float)
    }

    fun natureMediaPlayerInit(url: String, videoRenderType: Int, surface: Surface?) {
        mNativePlayHandle = native_init(url, videoRenderType, surface)
    }

    fun play() = native_play(mNativePlayHandle)

    fun pause() = native_pause(mNativePlayHandle)

    fun seekToPosition(position: Float) = native_seek2_position(mNativePlayHandle, position)

    fun stop() = native_stop(mNativePlayHandle)

    fun unInit() = native_un_init(mNativePlayHandle)

    fun getMediaParams(paramType: Int): Long = native_get_media_param(mNativePlayHandle, paramType)

    fun playerEventCallback(msgType: Int, msgValue: Float) =
        mEventCallback?.onPlayerEvent(msgType, msgValue)

    external fun native_get_media_param(mNativePlayHandle: Long, paramTypE: Int): Long

    external fun native_un_init(mNativePlayHandle: Long)

    external fun native_stop(mNativePlayHandle: Long)

    external fun native_seek2_position(mNativePlayHandle: Long, position: Float)

    external fun native_pause(mNativePlayHandle: Long)

    external fun native_init(url: String, videoRenderType: Int, surface: Surface?): Long

    external fun native_play(mNativePlayHandle: Long)

    external fun native_onsurface_created(renderType: Int)

    external fun native_ondraw_frame(renderType: Int)

    external fun native_onsurface_onchanged(renderType: Int, width: Int, height: Int)


}